package com.xyf.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xyf.sys.config.MiniprogramConfig;
import com.xyf.sys.domain.SysCart;
import com.xyf.sys.domain.SysCartItem;
import com.xyf.sys.service.ICartItemService;
import com.xyf.sys.service.ICartService;
import com.xyf.sys.util.R;
import com.xyf.sys.vo.CartItemAdd;
import com.xyf.sys.vo.CartListRequest;
import com.xyf.sys.vo.CartPreOrder;
import com.xyf.sys.vo.ShareOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author R-H
 * @Date 2024/4/151705
 * @Description
 */
@RestController
@RequestMapping("/cart")
@Slf4j
@Api(tags = "购物车管理")
public class CartController {

    @Autowired
    private ICartService cartService;
    @Autowired
    private ICartItemService cartItemService;
    @Autowired
    @Qualifier(value = "clientConfig")
    private MiniprogramConfig.MiniConfig clientConfig;

    @ApiOperation("查询购物车列表")
    @PostMapping("/list")
    public Object list(HttpServletRequest request) {
        String appid = request.getHeader("APPID");
        boolean client = clientConfig.getAppid().equals(appid);
        List<SysCartItem> list = cartItemService.getCartItemList(request.getHeader("openid"));
        return R.ok(list);
    }

 /*   @ApiOperation("查询默认购物车")
    @GetMapping("/get")
    public Object getDefault(@RequestParam(required = false) Long cartId, HttpServletRequest request) {
        String appid = request.getHeader("APPID");
        boolean client = clientConfig.getAppid().equals(appid);
        return R.ok(cartService.getUserCart(cartId, request.getHeader("openid"), client));
    }*/

    @ApiOperation("添加编辑购物车")
    @PostMapping("/add")
    public Object add(@RequestBody SysCartItem item, HttpServletRequest request) {
        String appid = request.getHeader("APPID");
        boolean client = clientConfig.getAppid().equals(appid);
        SysCart cart = cartService.add(item);
        return R.ok(cartItemService.getCartItemList(item.getUserId()));
    }

//    @ApiOperation("修改数量")
//    @PostMapping("/update")
//    public Object update(@RequestBody SysCartItem item, HttpServletRequest request) {
//        String appid = request.getHeader("APPID");
//        boolean client = clientConfig.getAppid().equals(appid);
//        SysCart cart = cartService.update(item);
//        return R.ok(cartService.getUserCart(cart.getId(), request.getHeader("openid"), client));
//    }

    @ApiOperation("删除购物车")
    @PostMapping("/delete")
    public Object deleteItem(@RequestBody SysCartItem body, HttpServletRequest request) {
        cartService.deleteItem(body.getId(), body.getOrderId(), body.getUserId());
        return R.ok(cartItemService.getCartItemList(body.getUserId()));
    }

    @ApiOperation("清空购物车")
    @PostMapping("/clear")
    public Object clear(HttpServletRequest request) {
        String appid = request.getHeader("APPID");
        boolean client = clientConfig.getAppid().equals(appid);
        cartItemService.clear(request.getHeader("openid"));
        return R.ok();
    }

    @ApiOperation("分享订单")
    @PostMapping("/share")
    public Object share(@RequestBody ShareOrder share, HttpServletRequest request) {
        String appid = request.getHeader("APPID");
        share.setAppid(appid);
        boolean client = clientConfig.getAppid().equals(appid);
        return R.ok(cartService.shareOrder(share));
    }


//    @ApiOperation("分享购物车")
//    @PostMapping("/preShare")
//    public Object preShare(@RequestBody ShareOrder shareOrder, HttpServletRequest request) {
//        shareOrder.setAppid(request.getHeader("APPID"));
//        return R.ok(cartService.preShare(shareOrder));
//    }

    @ApiOperation("购物车下单")
    @PostMapping("/preOrder")
    public Object preOrder(@RequestBody CartPreOrder cartPreOrder, HttpServletRequest request) {
        cartPreOrder.setAppid(request.getHeader("APPID"));
        cartPreOrder.setClient(clientConfig.getAppid().equals(cartPreOrder.getAppid()));
        return R.ok(cartItemService.createPreOrder(cartPreOrder));
    }
}
